const Award = require("./../model/Award");
const User = require("./../model/User")
const mongoose = require("mongoose");
//获取抽奖奖品
exports.getLotteryGoods = (req, res) => {
    const { activityId } = req.query;
    Award.find({isAvailable: true})
        .exec((err, result) => {
            if (err) {
                return res.send({ code: 500, message: "error" });
            }
            if (result) {
                let arr = [];
                for (let i = 0; i < result.length; i++) {
                    if(arr.length === 8){
                        break;
                    }
                    let obj = {};
                    obj.lottery_name = result[i].name;
                    obj.image_url = result[i].imgUrl;
                    obj.lottery_id = result[i]._id;
                    arr.push(obj);
                }
                return res.send({ code: 200, data: arr });
            } else {
                return res.send({ code: 200, data: [] });
            }
        });
};

exports.addLotteryGood = (req, res) => {
    const { name, amount, probability } = req.body;
    const createDate = new Date().format("yyyy-MM-dd");
    const isAvailable = false;
    const award = new Award({ name, amount, probability, createDate, isAvailable });
    award.save((err, doc) => {
        if (err) {
            return res.send({ code: 500, message: "error" });
        }
        console.log(doc)
        return res.send({ code: 200, message: "success" });
    });
}

exports.getConsumablesInfo = (req, res) => {
    User.find()
        .exec((err, result) => {
            if (err) {
                return res.send({ code: 500, message: "error" });
            }
            if (result) {
                let obj = {};
                obj.consumables_count = result[0].haveOre;
                obj.consume_count = result[0].needOre;
                return res.send({ code: 200, data: obj });
            } else {
                return res.send({ code: 200, data: [] });
            }
        });
}
//保存设置
exports.addConsumablesInfo = async (req, res) => {
    console.log(req.body)
    const { gift_list, gift_count, gift_used_count } = req.body;
    if(gift_list === undefined || gift_count === undefined || gift_used_count === undefined){
        return res.send({ code: 500, message: "数据不能提交undefined" });
    }
    // console.log(gift_list)
    // console.log(gift_count)
    // console.log(gift_used_count)
    if(gift_list.length < 8){
        return res.send({ code: 500, message: "请提交8个奖品" });
    }
    const updateResult = await Award.update({}, {isAvailable: false});
    await Award.updateMany({_id: {$in: gift_list}}, {isAvailable: true});
    // for(let i = 0; i < gift_list.length; i++){
    //     Award.update()
    // }
    const users = await User.find({});
    if (users.length < 1) {
        const user = new User({ username: "xlk", haveOre: gift_count, needOre: gift_used_count });
        user.save((err, doc) => {
            if (err) {
                return res.send({ code: 500, message: "添加失败" });
            }
            return res.send({ code: 200, message: "添加成功" });
        });
    } else {
        User.updateOne({ _id: users[0]._id }, { haveOre: gift_count, needOre: gift_used_count }, function (err, raw) {
            if (err) {
                return res.send({ code: 500, message: "修改失败" });
            }
            return res.send({ code: 200, message: "修改成功" });
        })
    }

}
//计算中奖结果
exports.getLotteryResult = (req, res) => {
    Award.find({isAvailable: true})
        .exec((err, result) => {
            if (err) {
                return res.send({ code: 500, message: "error" });
            }
            if (result) {
                let index = parseInt((Math.random() * 100) % result.length);
                console.log(index)
                return res.send({ code: 200, data: result[index]._id });
            } else {
                return res.send({ code: 200, data: [] });
            }
        });
}

//所有奖品
exports.getAllLotteryGoods = (req, res) => {
    Award.find()
        .exec((err, result) => {
            if (err) {
                return res.send({ code: 500, message: "error" });
            }
            if (result) {
                let arr = [];
                for (let i = 0; i < result.length; i++) {
                    let obj = {};
                    obj.lottery_name = result[i].name;
                    // obj.image_url = "http://lottery.vaiwan.com/img/img_" + i + ".webp";
                    obj.image_url = result[i].imgUrl;
                    obj.lottery_id = result[i]._id;
                    obj.lottery_probability = result[i].probability;
                    arr.push(obj);
                }
                return res.send({ code: 200, data: arr });
            } else {
                return res.send({ code: 200, data: [] });
            }
        });
}